package com.tistory.login.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tistory.admin.adminmgr.auth.model.LoginUserInfo;
import com.tistory.common.vo.CommonReq;
import com.tistory.login.vo.MemberDocuRes;
import com.tistory.login.vo.MemberRes;
import com.tistory.login.vo.MemberSearchInfo;

@Repository("LoginDaoImpl")
public class LoginDaoImpl implements LoginDao {

	@Autowired
	SqlMapClientTemplate sqlMapClientTemplate;

	public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
		this.sqlMapClientTemplate = sqlMapClientTemplate;
	}


	@Override
	public LoginUserInfo getUser(Map<String,Object> map) throws SQLException {

		return (LoginUserInfo) sqlMapClientTemplate.queryForObject("LoginSql.getUser", map);
	}




	@Override
	public List<MemberRes> getMemberList(CommonReq req) throws SQLException {
		List<MemberRes> mmberList = sqlMapClientTemplate.queryForList("LoginSql.getMemberList",  req);
		return mmberList;
	}


	@Override
	public Integer getMemberListCount(CommonReq req) throws SQLException {
		return (Integer) sqlMapClientTemplate.queryForObject("LoginSql.getMemberListCount", req);
	}


	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer insertUserInfo(Map<String, Object> map) throws SQLException {
		Integer kk = 0;
		if((Integer)sqlMapClientTemplate.update("LoginSql.insertUserInfo", map)>0){
			if(map.get("rightCdFlag").equals("02")){

				sqlMapClientTemplate.insert("LoginSql.insertStroreInfo", map);
				sqlMapClientTemplate.insert("LoginSql.insertStroreIpkumNumInfo", map);
			}
		}
		return kk;
	}


	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer updateUserInfo(Map<String, Object> map) throws SQLException {
		Integer kk = 0;
		if((Integer)sqlMapClientTemplate.update("LoginSql.updateUserInfo", map)>0){
			if(map.get("rightCdFlag").equals("02")){
				sqlMapClientTemplate.update("LoginSql.updateStroreInfo", map);
				 kk = (Integer)sqlMapClientTemplate.update("LoginSql.updateStroreIpkumNumInfo", map);
				 if(kk==0){
					 sqlMapClientTemplate.insert("LoginSql.insertStroreIpkumNumInfo", map);
				 }
			}
		}
		return kk;
	}


	@Override
	public MemberSearchInfo getMemberSearch(String userId) throws SQLException {
		MemberSearchInfo searchInfo = new MemberSearchInfo();
		searchInfo =(MemberSearchInfo) sqlMapClientTemplate.queryForObject("LoginSql.getMemberSearch", userId);
		return searchInfo;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer deleteUserInfo(Map<String, Object> map) throws SQLException {
		Integer kk = 0;
		if((Integer)sqlMapClientTemplate.delete("LoginSql.deleteUserInfo", map)>0){
			if(map.get("rightCdFlag").equals("02")){
				sqlMapClientTemplate.delete("LoginSql.deleteStroreInfo", map);
			}
		}
		return kk;
	}


	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void insertFileInfo(Map<String, Object> map) throws SQLException {
		Integer kk = 0;
		kk = (Integer) sqlMapClientTemplate.queryForObject("LoginSql.getFileInfoCount", map);
		if( kk >0){
			sqlMapClientTemplate.update("LoginSql.updateFileInfo", map);
		}else{
			Integer i = sqlMapClientTemplate.update("LoginSql.insertFileInfo", map);
			if(i >0){
				sqlMapClientTemplate.update("LoginSql.updateFileInfo", map);
			}
		}
	}


	@Override
	public MemberDocuRes getFileInfo(Map<String, Object> map) throws SQLException {
		// TODO Auto-generated method stub
		return (MemberDocuRes) sqlMapClientTemplate.queryForObject("LoginSql.getFileInfo", map);
	}
}
